package com.tuyrt.`as`.proj.main.biz.goods

import android.os.Bundle
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.tuyrt.`as`.proj.common.ui.component.HiBaseActivity
import com.tuyrt.`as`.proj.main.R
import com.tuyrt.`as`.proj.main.route.HiRoute
import com.tuyrt.hi.library.util.HiDataBus
import com.tuyrt.hi.library.util.HiStatusBar
import kotlinx.android.synthetic.main.activity_goods_list.*

@Route(path = "/goods/list")
class GoodsListActivity : HiBaseActivity() {

    @JvmField
    @Autowired
    var categoryTitle: String = ""

    @JvmField
    @Autowired
    var categoryId: String = ""

    @JvmField
    @Autowired
    var subcategoryId: String = ""

    private val FRAGMENT_TAG = "goods_list_fragment"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        HiStatusBar.setStatusBar(this, true, translucent = false)
        setContentView(R.layout.activity_goods_list)
        HiRoute.inject(this)

        action_back.setOnClickListener { onBackPressed() }
        category_title.text = categoryTitle

        var fragment = supportFragmentManager.findFragmentByTag(FRAGMENT_TAG)
        if (fragment == null) {
            fragment = GoodsListFragment.newInstance(categoryId, subcategoryId)
        }
        val ft = supportFragmentManager.beginTransaction()
        if (!fragment.isAdded) {
            ft.add(R.id.container, fragment, FRAGMENT_TAG)
        }
        ft.show(fragment).commitNowAllowingStateLoss()

        // 测试 HiDataBus
//        HiDataBus.with<String>("stickyData").observeSticky(this, true) {
//            Log.e("aaaa", "receive stickyData: $it")
//            toast("receive stickyData: $it")
//        }
    }
}